package com.gitee.wsl.common.ui.ext

import androidx.compose.ui.graphics.Matrix
import org.jetbrains.skia.Matrix33
import kotlin.math.abs


fun Matrix.asSkia33(coerceScale : Boolean = false) : Matrix33 {

    // skiko shaders with zero scale cause crash

    val scaleX = when {
        coerceScale && abs(values[Matrix.ScaleX]) < 0.001f -> 0.001f
        else -> values[Matrix.ScaleX]
    }

    val scaleY = when {
        coerceScale && abs(values[Matrix.ScaleY]) < 0.001f -> 0.001f
        else -> values[Matrix.ScaleY]
    }


    return Matrix33(
        scaleX,
        values[Matrix.SkewX],
        values[Matrix.TranslateX],
        values[Matrix.SkewY],
        scaleY,
        values[Matrix.TranslateY],
        values[Matrix.Perspective0],
        values[Matrix.Perspective1],
        values[Matrix.Perspective2],
    )
}